package ro.ubbcluj.cs.damate.plugin.ws.service;

import ro.ubbcluj.cs.damate.plugin.events.EventType;
import ro.ubbcluj.cs.damate.plugin.events.PluginEvent;
import ro.ubbcluj.cs.damate.plugin.events.ResourceEvent;
import ro.ubbcluj.cs.damate.plugin.ws.generated.EventParameter;
import ro.ubbcluj.cs.damate.plugin.ws.generated.File;
import ro.ubbcluj.cs.damate.plugin.ws.generated.PluginEventRequest;
import ro.ubbcluj.cs.damate.plugin.ws.generated.Project;
import ro.ubbcluj.cs.damate.plugin.ws.generated.Workspace;

import static ro.ubbcluj.cs.damate.plugin.utils.PluginEventConstants.*;

/**
 * @author Lóránd Fazakas (lfazakas)
 *         Date: 2013.06.28.
 *         Time: 12:11
 */
public class RequestResponseBuilder {

    public static PluginEventRequest buildPluginEventRequest(PluginEvent pluginEvent) {
        PluginEventRequest request = null;
        if (pluginEvent.getType() == EventType.WORKSPACE) {
            request = buildWorkspaceEventRequest(pluginEvent);
        } else if (pluginEvent.getType() == EventType.RESOURCE) {
            request = buildResourceEventRequest(pluginEvent);
        } else if (pluginEvent.getType() == EventType.COMMAND) {
            request = new PluginEventRequest();
            Workspace entity = new Workspace();
            entity.setPath(pluginEvent.getParameters().get(WORKSPACE));
            request.setObservedEntity(entity);
            EventParameter commandType = new EventParameter();
            commandType.setName(COMMAND);
            commandType.setValue(pluginEvent.getParameters().get(COMMAND_ID));
            request.getParameters().add(commandType);
        }
        request.setEventTypeCode(getEventCodeByAction(pluginEvent));
        return request;
    }

    private static PluginEventRequest buildWorkspaceEventRequest(PluginEvent pluginEvent) {
        PluginEventRequest request = new PluginEventRequest();
        Workspace entity = new Workspace();
        entity.setPath(pluginEvent.getParameters().get(WORKSPACE));
        request.setObservedEntity(entity);
        return request;
    }

    private static PluginEventRequest buildResourceEventRequest(PluginEvent pluginEvent) {
        PluginEventRequest request = new PluginEventRequest();
        Workspace workspace = new Workspace();
        workspace.setPath(pluginEvent.getParameters().get(WORKSPACE));
        Project project = new Project();
        project.setParent(workspace);
        project.setName(pluginEvent.getParameters().get(PROJECT));
        if (((ResourceEvent)pluginEvent).isProject()) {
            request.setObservedEntity(project);
        } else {
            File file = new File();
            file.setParent(project);
            file.setPath(pluginEvent.getParameters().get(RESOURCE));
            file.setType(((ResourceEvent) pluginEvent).getResourceType());
            request.setObservedEntity(file);
        }
        return request;
    }

    private static int getEventCodeByAction(PluginEvent pluginEvent) {
        return pluginEvent.getType().getEventTypeCode() + getEventCode(pluginEvent.getWhat());
    }
}
